<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--form上传文件-->
<h1>验证码</h1>
<!--点击图片刷新-->
<img src="/kaptcha" id="kaptcha" onclick="document.getElementById('kaptcha').setAttribute('src','/kaptcha')">
<!--点击按钮刷新-->
<button id="refreshkaptcha" onclick="document.getElementById('kaptcha').setAttribute('src','/kaptcha')">刷新</button>

<hr/>
<h2>1.导入依赖</h2>
<textarea rows="6" cols="140">
    <dependency>
        <groupId>com.github.penggle</groupId>
        <artifactId>kaptcha</artifactId>
        <version>${kaptcha.version}</version>
    </dependency>
</textarea>

<h2>2.配置config文件</h2>
<textarea rows="10" cols="140">
<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
        <property name="config">
            <bean class="com.google.code.kaptcha.util.Config">
                <constructor-arg>
                    <props>
                        <prop key="kaptcha.border">yes</prop>
                        <prop key="kaptcha.border.color">105,179,90</prop>
                        <prop key="kaptcha.textproducer.font.color">blue</prop>
                        <prop key="kaptcha.image.width">125</prop>
                        <prop key="kaptcha.image.height">45</prop>
                        <prop key="kaptcha.textproducer.font.size">45</prop>
                        <prop key="kaptcha.session.key">code</prop>
                        <prop key="kaptcha.textproducer.char.length">4</prop>
                        <prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>
                    </props>
                </constructor-arg>
            </bean>
        </property>
    </bean>
</beans>
</textarea>

<h2>3.提供Controller(/kaptcha)</h2>
<textarea rows="10" cols="140">
@Controller
public class KaptchaController {
    @Autowired
    private Producer captchaProducer;
    @RequestMapping(value = "kaptcha", method = RequestMethod.GET)
    public ModelAndView verification(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setDateHeader("Expires", 0);
        // Set standard HTTP/1.1 no-cache headers.
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        // Set standard HTTP/1.0 no-cache header.
        response.setHeader("Pragma", "no-cache");
        // return a jpeg
        response.setContentType("image/jpeg");
        // create the text for the image
        String capText = captchaProducer.createText();
        // store the text in the session
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
        // create the image with the text
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        // write the data out
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
        return null;
    }
}
</textarea>

<h2>4.(视情况而定)拦截器中需要放开对/kaptcha的登录拦截</h2>
<textarea rows="10" cols="140">
    <!--session拦截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/login"/>
            <mvc:exclude-mapping path="/kaptcha"/>
            <mvc:exclude-mapping path="/static/**"/>
            <mvc:exclude-mapping path="${file.staticAccessPath}/**"/>
            <bean class="com.min.spring.interceptor.SessionInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
</textarea>


<h2>5.HTML中的使用</h2>
<textarea rows="4" cols="140">
    <!--点击图片刷新-->
    <img src="/kaptcha" id="kaptcha" onclick="document.getElementById('kaptcha').setAttribute('src','/kaptcha')">
    <!--点击按钮刷新-->
    <button id="refreshkaptcha" onclick="document.getElementById('kaptcha').setAttribute('src','/kaptcha')">刷新</button>

</textarea>

</body>


</html>